from flask import request, jsonify
from flask.views import MethodView
from app.models.orders import Orders
from webargs import fields
from webargs.flaskparser import parser

parser_args = {
    'page': fields.Int(missing=0, error_messages={'invalid': '必须是数值类型'})
}


class OrderAPI(MethodView):
    def get(self, id):
        data = Orders.query.get_or_404(id).to_dict()
        return jsonify(success=True, data=data)

class OrdersAPI(MethodView):
    def get(self):
        args = parser.parse(parser_args, request)
        data = Orders.to_collection_dict(Orders.query, args['page'])
        return jsonify(success=True, data=data)
